import Vue from "vue";
import VueRouter from "vue-router";
import Layout from "@/layout";
import { isPermission } from "@/utils/auth.js";

Vue.use(VueRouter);
const originalPush = VueRouter.prototype.push;

VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch((err) => err);
};

const routes = [
  {
    path: "/404",
    hidden: false,
    name: "404",
    hasChild: false,
    component: () => import("@/views/404.vue"),
    meta: {
      title: "404",
      icon: "el-icon-s-check",
    },
  },
  {
    path: "/data-entry",
    component: Layout,
    hidden: isPermission("s2"),
    meta: {
      title: "数据录入",
      icon: "el-icon-s-check",
    },
    children: [
      {
        path: "", // 默认子路由
        name: "DataEntry",
        component: () => import("@/views/DataEntry/index.vue"),
        meta: { icon: "el-icon-s-check", module: "s2" },
      },
      {
        path: "new-data", // 相对路径
        name: "NewData",
        hidden: false,
        component: () => import("@/views/DataEntry/NewData.vue"),
        meta: { title: "添加数据", icon: "el-icon-s-check", module: "s2" },
      },
    ],
  },

  {
    path: "/interface-management",
    component: Layout,
    hidden: isPermission("s5"),
    hasChild: false,
    meta: { title: "断面管理", icon: "el-icon-s-data", module: "s5" },
    children: [
      {
        path: "/interface-management",
        name: "InterfaceManagement",
        component: () => import("@/views/InterfaceManagement/index.vue"),
        meta: { icon: "el-icon-s-data", module: "s5" },
      },
    ],
  },
  {
    path: "/data-audit",
    component: Layout,
    hidden: isPermission("s3"),
    hasChild: false,
    meta: {
      title: "数据审核",
      icon: "el-icon-s-check",
    },
    children: [
      {
        path: "/data-audit",
        name: "DataAudit",
        component: () => import("@/views/DataAudit/index.vue"),
        meta: { icon: "el-icon-s-check", module: "s3" },
      },
    ],
  },
  {
    path: "/statistics",
    component: Layout,
    hidden: isPermission("s3"),
    hasChild: true,
    meta: { title: "数据统计", icon: "el-icon-s-data", module: "s3" },
    children: [
      {
        path: "/statistics/CompensationStatistics",
        name: "CompensationStatistics",
        component: () => import("@/views/Statistics/CompensationStatistics.vue"),
        meta: { title: "补偿金统计", icon: "el-icon-s-data", module: "s3" },
      },
      {
        path: "/statistics/ScoreDeductionStatistics",
        name: "ScoreDeductionStatistics",
        component: () => import("@/views/Statistics/ScoreDeductionStatistics/ScoreDeductionStatistics.vue"),
        meta: { title: "得扣分统计", icon: "el-icon-s-data", module: "s3" },
      },
    ],
  },
];

const router = new VueRouter({
  routes,
});

router.beforeEach((to, from, next) => {
  if (isPermission(to.meta.module)) {
    next();
  } else if (to.name == "404") {
    next();
  } else {
    router.push({ name: "404" });
  }
});

export default router;
